package com.zf.shuashua.ui.gift;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import androidx.lifecycle.ViewModelProviders;

import com.zf.shuashua.ui.record.AlivcRecordSettingActivity;
import com.zf.shuashua.R;
import com.zf.shuashua.ShuaApplication;
import com.zf.shuashua.api.Constants;
import com.zf.shuashua.base.BaseFragment;
import com.zf.shuashua.common.ScreenUtils;
import com.zf.shuashua.ui.custom.H5Fragment2;

public class GiftFragment extends BaseFragment{

    private GiftViewModel giftViewModel;

    private static final String INTENT_PARAM_KEY_VALUE = "svideo";

    private Button mShotButton;

    private H5Fragment2 mH5Fragment;
    private boolean isFirst;
    private boolean isShowAd;

    @Override
    protected void initView(View root) {
        root.setPadding(0, ScreenUtils.getStatusBarHeight(ShuaApplication.getContext()), 0, 0);
        isFirst = true;
        mShotButton = root.findViewById(R.id.shot);
        mShotButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 视频拍摄
                Intent record = new Intent(getActivity(), AlivcRecordSettingActivity.class);
                //判断是编辑模块进入还是通过社区模块的编辑功能进入
                //svideo: 短视频
                //community: 社区
                record.putExtra(com.aliyun.svideo.base.AlivcSvideoEditParam.INTENT_PARAM_KEY_ENTRANCE, INTENT_PARAM_KEY_VALUE);
                startActivity(record);
            }
        });
        mH5Fragment = H5Fragment2.newInstance(Constants.H5_URL);
        getChildFragmentManager().beginTransaction().add(R.id.framelayout, mH5Fragment).commit();
    }

    @Override
    protected void initViewModel() {
        giftViewModel = ViewModelProviders.of(this).get(GiftViewModel.class);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_gift;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (mH5Fragment != null && !isFirst) {
                if (mH5Fragment.isFromReward) {
                    mH5Fragment.isFromReward = false;
                } else {
                    mH5Fragment.reload(); // 刷新
                    if(!isShowAd){
                        mH5Fragment.showAd();
                        isShowAd = true;
                    }
                }
            } else {
                isFirst = false;
            }
        }
    }

    @Override
    public void onResume() {
//        if (mH5Fragment != null) {
//            mH5Fragment.reload(); // 刷新
//        }
        super.onResume();

    }

    public boolean isShowPopup(){
        if(mH5Fragment == null){
            return false;
        }
        return mH5Fragment.isShowPopup();
    }

    public boolean isConsume(int keyCode, KeyEvent event){
        if(mH5Fragment == null){
            return false;
        }
        return mH5Fragment.isConsume(keyCode, event);
    }

    public void scrollToModle(String modName){
        if(mH5Fragment != null){
            mH5Fragment.scrollToModle(modName);
        }
    }

//    @Override
//    public boolean onFragmentKeyDown(int keyCode, KeyEvent event) {
//        return mH5Fragment.getWeb().handleKeyEvent(keyCode, event);
//    }
}